#include "stm32f10x.h"
#include "bsp_led.h"
#include "systick.h"
#include "bsp_buzz.h"   
#include "bsp_key.h"
#include "bsp_num.h"


static int16_t i=0;
uint8_t KEY1_SCAN(void)
{
	static uint8_t key1_old=1;
	uint8_t key1_now;
	key1_now=GETKEY1();
	if(key1_old==1 &&key1_now==0)
	{
		key1_old=key1_now;return 1;
	}
	else 
	{
		key1_old=key1_now;return 0;
	}
}
uint8_t KEY2_SCAN(void)
{
	static uint8_t key2_old=1;
	uint8_t key2_now;
	key2_now=GETKEY2();
	if(key2_old==1 &&key2_now==0)
	{
		key2_old=key2_now;return 1;
	}
	else 
	{
		key2_old=key2_now;return 0;
	}
}
uint8_t KEY_Micro_SCAN(void)
{
	static uint8_t key_micro_old=1;
	uint8_t key_micro_now;
	key_micro_now=GETKEY_Micro();
	if(key_micro_old==1 && key_micro_now==0)
	{
		key_micro_old=key_micro_now;return 1;
	}
	else 
	{
		key_micro_old=key_micro_now;return 0;
	}
}
static uint8_t key1_pressed=0;
void KEY1_SCAN_FLAG(void)
{
	static uint8_t key1_old = 1;
	uint8_t key1_now;
	key1_now=GETKEY1();
	if(key1_old==1 &&key1_now==0)
		key1_pressed=1;
	key1_old=key1_now;
}
static uint8_t key2_pressed=0;
void KEY2_SCAN_FLAG(void)
{
	static uint8_t key2_old = 1;
	uint8_t key2_now;
	key2_now=GETKEY2();
	if(key2_old==1 &&key2_now==0)
		key2_pressed=1;
	key2_old=key2_now;
}
static uint8_t key_micro_pressed=0;
void KEY_Micro_SCAN_FLAG(void)
{
	static uint8_t key_micro_old = 1;
	uint8_t key_micro_now;
	key_micro_now=GETKEY_Micro();
	if(key_micro_old==1 &&key_micro_now==0)
		key_micro_pressed=1;
	key_micro_old=key_micro_now;
}

void BUZZ_Once_ON(void)
{
    BUZZ_ON();
    SysTick_Delay_Nms(500);  
    BUZZ_OFF();
}
int main(void)
{
	KEY_GPIO_Config();
	NUM_GPIO_Config();		
	BUZZ_GPIO_Config();
	BUZZ_OFF();
	while(1)
	{
		SysTick_Delay_Nms(5);
		KEY1_SCAN_FLAG();	
		KEY2_SCAN_FLAG();
		KEY_Micro_SCAN_FLAG();
		if(key1_pressed==1)
		{
			key1_pressed=0;
			i++;
		}
		if(key2_pressed==1)
		{
			key2_pressed=0;
			i--;
		}
		if(key_micro_pressed==1)
		{
			key_micro_pressed=0;
			i=0;
		}
		Send_595(i);	
		if(i>15)
			i=0;
		if(i<0)
			i=15;
		
	}
}

